<?php
namespace model;
use Core\common\Auth;
use Core\common\Data;
class menu extends \Core\common\Model
{

    /**
     * 对数组排序
     *
     * @param [type]  $arr   [数组]
     * @param integer $pid [pid]
     * @param integer $level [层级]
     *
     *
     * @author luzehua
     * @date   2018-05-13
     *
     */
    public function GetCateList($arr, $root_id = 0, $level = 1)
    {
        if (!$arr) return;
        $tree = array();
        foreach ($arr as $v) {
            if ($v['pid'] == $root_id) {
                $level == 1 && $v['pname'] = '├─' .$v['name'];
                $level == 2 && $v['pname'] = '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'.'├─' . $v['name'];
                $level >= 3 && $v['pname'] = str_repeat('&nbsp;&nbsp;&nbsp;&nbsp;', $level + 2) . '&nbsp;&nbsp;&nbsp;&nbsp;├─' . $v['name'];
                $v['level'] = $level;
                $tree[] = $v;
                $tree = array_merge($tree, $this->GetCateList($arr, $v['id'], $level + 1));
            }
        }
        return $tree;
    }

    /**
     * 获取全部菜单
     * @param string $type tree获取树形结构 level获取层级结构
     * @return array        结构数据
     */
    public function getTreeData($type = 'tree')
    {
        global $_W;
        $data = $this->top(['where' => ['status'=>1], 'order' =>['order_num desc']]);
        $datas = new Data();

        $aParams['field'] = ['create_uid'];
        $aParams['where'] = ['uniacid'=>uniacid()];
        $create_uid = M('uni_account',false)->getData($aParams);

        // 获取树形或者结构数据
        if ($type == 'tree') {

            $data = $datas::tree($data, 'name', 'id', 'pid');

        } elseif ($type = "level") {

            $data = $datas::channelLevel($data, 0, '&nbsp;', 'id');

            // 显示有权限的菜单
            $auth = new Auth();
            if($create_uid['create_uid'] != $_W['uid']){
                foreach ($data as $k => $v) {

                    if ($auth->check($v['mca'], $_W['uid'])) {
                        foreach ($v['_data'] as $m => $n) {
                            if (!$auth->check($n['mca'], $_W['uid'])) {
                                unset($data[$k]['_data'][$m]);
                            }
                        }
                    } else {
                        // 删除无权限的菜单
                        unset($data[$k]);
                    }
                }
            }

        }
        return $data;
    }
}